import { createRouter, createWebHistory } from "vue-router";

const routes = [
    {
        path: "/",
        name: "welcome",
        component: () => import("@/views/welcome.vue")
    },
    {
        path: "/home",
        name: "home",
        component: () => import("@/views/user/home.vue"),
        children: [
            {
                path: "videoBox",
                name: "videoBox",
                component: () => import("@/views/user/videoBox.vue")
            }, {
                path: "recommendBox",
                name: "recommendBox",
                component: () => import("@/views/user/recommendBox.vue")
            },
            {
                path: "regardBox",
                name: "regardBox",
                component: () => import("@/views/user/regardBox.vue")
            }]
    },
    {
        path: "/login",
        name: 'login',
        component: () => import("@/views/user/login.vue")
    },
    {
        path: "/myself",
        name: 'myself',
        component: () => import("@/views/user/myself.vue")
    },
    {
        path: "/usercenter",
        name: 'usercenter',
        component: () => import("@/views/user/usercenter.vue")
    },
    {
        path:'/:catchAll(.*)',
        component:() => import("@/views/user/404.vue")
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    if(to.name != "login"){
    if (to.name == "myself") {
        if(!window.localStorage.getItem("token"))
        next({ name: 'login' })}
    // 如果用户未能验证身份，则 `next` 会被调用两次
    }next()
  })

export default router;

